package com.witsight.platform.aspect;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.core.annotation.Order;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import com.witsight.platform.Constants;
import com.witsight.platform.annotation.logging.General;
import com.witsight.platform.annotation.logging.Important;
import com.witsight.platform.annotation.logging.Logging;
import com.witsight.platform.annotation.logging.Serious;
import com.witsight.platform.util.lang.StringUtil;

/**   
 * 说明：服务切面日志记录
 * @Title: Log4j2AspectHandler.java 
 * @Package com.witsight.platform.aspect 
 * @See: {@link Log4jAspectHandler}<br/>
 * Copyright: Copyright (c) 2017<br/>
 * Company:sany huax witsight team by product
 * @author: penght  
 * @date: 2017年10月31日 下午2:08:56 
 * @version: V1.0
 *
 */
@Aspect
@Order(com.witsight.platform.Constants.LOGGING_ORDER)
@Component
@ConditionalOnClass({ JoinPoint.class})
public class Log4jAspectHandler extends LoggingAspect {
	@Pointcut("execution(public * com..*.service..*.*(..))")
	@Override
	public void logging() {
	}

	@Override
	protected void writeLog(JoinPoint joinPoint, String content, Throwable ex) throws Throwable {
		if (joinPoint == null || joinPoint.getTarget() == null) {
			return;
		}
		Class<?> clazz = joinPoint.getTarget().getClass();
		if (clazz == null) {
			return;
		}
		
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();
		Logging logging = clazz.getAnnotation(Logging.class);
		method = clazz.getMethod(method.getName(), method.getParameterTypes());
		General general = method.getAnnotation(General.class);
		Important important = method.getAnnotation(Important.class);
		Serious serious = method.getAnnotation(Serious.class);
		// 取得日志对象
		final Logger logger = this.getLogger(clazz);
		String model = StringUtil.getModel(logger.getName(), "SERVICE");
		if (general != null && ex == null && logger.isDebugEnabled()) {
			logger.debug(Constants.AUTO_MARKER,  content, model, method.toGenericString(), joinPoint.getArgs(), general.value());
		} else if (important != null && ex == null && logger.isInfoEnabled()) {
			logger.info(Constants.AUTO_MARKER,content, model, method.toGenericString(), joinPoint.getArgs(), important.value());
		} else if (serious != null && ex == null && logger.isWarnEnabled()) {
			logger.warn(Constants.AUTO_MARKER,content, model, method.toGenericString(), joinPoint.getArgs(), serious.value());
		} else if (logging != null && ex == null && logger.isDebugEnabled()) {
			logger.debug(Constants.AUTO_MARKER,content, model, method.toGenericString(), joinPoint.getArgs(), logging.value());
		} else if (ex == null && logger.isDebugEnabled()){
			logger.debug(Constants.AUTO_MARKER,content, model, method.toGenericString(), joinPoint.getArgs(), "service logic");
		}
		if (ex != null && logger.isErrorEnabled()) {
			logger.error(Constants.AUTO_MARKER, content, model, method.toGenericString(), joinPoint.getArgs(),"exception: ");
			logger.error(Constants.AUTO_MARKER, "error : ", ex);
		}
	}
	
}
